﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ZoomPictureBoxTest
{
    public partial class TestForm : Form
    {
        private ZoomPictureBox.ZoomPictureBox zoomPictureBox;
        private Button btnReset;
        private Button btnLoadImage;

        public TestForm()
        {
            InitializeComponent();
            CreateControls();
            SetupLayout();
        }

        private void CreateControls()
        {
            zoomPictureBox = new ZoomPictureBox.ZoomPictureBox
            {
                Dock = DockStyle.Fill,
                BackColor = Color.DarkGray
            };

            btnReset = new Button
            {
                Text = "重置视图",
                Dock = DockStyle.Bottom,
                Height = 30
            };
            btnReset.Click += (s, e) => zoomPictureBox.ResetView();

            btnLoadImage = new Button
            {
                Text = "加载图片",
                Dock = DockStyle.Bottom,
                Height = 30
            };
            btnLoadImage.Click += BtnLoadImage_Click;

            Controls.AddRange(new Control[] { zoomPictureBox, btnReset, btnLoadImage });
        }

        private void SetupLayout()
        {
            Text = "图像查看器（右键拖动，滚轮缩放）";
            Size = new Size(800, 600);
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void BtnLoadImage_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                dlg.Filter = "图片文件|*.jpg;*.png;*.bmp";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        zoomPictureBox.Image?.Dispose();
                        zoomPictureBox.Image = Image.FromFile(dlg.FileName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"加载失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}